package com.supersit.greensproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.activity.BaseInfoEditActivity;
import com.supersit.greensproject.entity.BaseBase;
import com.supersit.greensproject.utils.T;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/8/8 0008.
 */
public class BaseSynchroizationAdapter extends BaseAdapter {
    private Context context;
    private List<BaseBase> baseList;
    private static HashMap<Integer, Boolean> isSelected;

    public BaseSynchroizationAdapter(List<BaseBase> baseList, Context context) {
        this.baseList = baseList;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        if (null != baseList && baseList.size() > 0) {
            for (int i = 0; i < baseList.size(); i++) {
                getIsSelected().put(i, false);
            }
        }
    }

    @Override
    public int getCount() {
        return baseList == null ? 0 : baseList.size();
    }

    @Override
    public Object getItem(int position) {
        return baseList == null ? null : baseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_base_synchroization, null);
            holder = new ViewHolder();
            holder.cbSelected = (CheckBox) view.findViewById(R.id.cb_selected);
            holder.tvArea = (TextView) view.findViewById(R.id.tv_area);
            holder.tvBaseName = (TextView) view.findViewById(R.id.tv_base);
            holder.tvStatus = (TextView) view.findViewById(R.id.tv_proportion);
            holder.llSecondMenu = (LinearLayout) view.findViewById(R.id.ll_second_menu);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // 根据isSelected来设置checkbox的选中状况

        if (null != isSelected && isSelected.size() > 0) {
            holder.cbSelected.setChecked(getIsSelected().get(position));
        } else {
            holder.cbSelected.setChecked(false);
        }
        holder.tvArea.setText(context.getResources().getStringArray(R.array.area)[baseList.get(position).getAreaId()]);
        holder.tvBaseName.setText(baseList.get(position).getBaseName());
        holder.tvStatus.setText(context.getResources().getStringArray(R.array.stutas)[baseList.get(position).getOperationsTatus()]);
        holder.llSecondMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseBase baseBase = baseList.get(position);
                Intent intent = new Intent(context, BaseInfoEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("basebase", baseBase);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        holder.cbSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    getIsSelected().put(position, true);
                } else {
                    getIsSelected().put(position, false);
                }
            }
        });

        return view;
    }

    class ViewHolder {
        CheckBox cbSelected;
        TextView tvBaseName;
        TextView tvArea;
        TextView tvStatus;
        LinearLayout llSecondMenu;
    }

    public void setData(List<BaseBase> baseList) {
        this.baseList = baseList;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        BaseSynchroizationAdapter.isSelected = isSelected;
    }
}
